package com.exonline.front.controller.subject;

import com.exonline.commons.base.IBaseService;
import com.exonline.commons.base.IBaseServlet;
import com.exonline.commons.dao.DaoImpl;
import com.exonline.commons.dao.IDao;
import com.exonline.commons.entity.Subject;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @desc   M层
 */

public class subjectService implements IBaseService<Subject> {

    IDao<Subject> subjectDao = new DaoImpl<>();

    /**
     * @desc  查询全部
     * @param pageSize
     * @param pageNo
     * @return
     * @throws SQLException
     */
    @Override
    public List<Subject> queryAll(int pageSize, int pageNo) throws SQLException {
        return subjectDao.queryWithPagingAndConditions("SELECT * FROM subject",null,pageNo,pageSize,new BeanListHandler<>(Subject.class));
    }

    /**
     * @desc  根据id查询一个
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public Subject queryOne(String id) throws SQLException {
        return   subjectDao.queryForObject("select * from subject where id = ?", new BeanHandler<>(Subject.class), id);
    }

    /**
     * @param paras
     * @return
     * @throws SQLException
     * @desc 添加
     */
    @Override
    public void add(Object... paras) throws SQLException {
        subjectDao.updateWithTransaction("INSERT INTO subject VALUES(?,?)",paras);
    }

    /**
     * @desc  修改
     * @param paras
     * @throws SQLException
     */
    @Override
    public void update(Object... paras) throws SQLException {
        subjectDao.updateWithTransaction("UPDATE subject SET suname = ? WHERE id = ?",paras);
    }

    /**
     * @desc  删除
     * @param id
     * @throws SQLException
     */
    @Override
    public void deleteById(String id) throws SQLException {
        subjectDao.updateWithTransaction(" DELETE FROM subject WHERE id = ?",id);
    }
}
